package cn.zhizihua.domain.query;

import cn.zhizihua.domain.po.Business;
import cn.zhizihua.domain.po.Food;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Data;

/**
 * 在分页的基础上，添加食品名称的模糊查询
 */
@Data
public class FoodQuery extends BaseQuery{
    private String foodName; //食品名称模糊搜索
    private Integer businessId;  // 商家编号

    /*
      select f.foodId, f.foodName, f.foodExplain, foodImg, foodPrice, f.businessId, f.remarks ,
                    b.businessName as 'business.businessName'
             from t_food f left join t_business b on f.businessId=b.businessId
             <if test="ew.sqlSegment!=null and ew.sqlSegment!=''">
                 where ${ew.sqlSegment}
             </if>
             order by foodId desc
     */

    //多表查询  查询餐品信息及其对应商家
    @Override
    public Wrapper getQueryWrapper() {
        MPJLambdaWrapper<Food> queryWrapper = new MPJLambdaWrapper<Food>()
                .setAlias("f") //为food表设置别名
                .selectAll(Food.class, "f") //查询food表全部信息
                .selectAs(Business::getBusinessName, "'business.businessName'") //联查business表businessName
                .selectAs(Business::getBusinessAddress, "'business.businessAddress'") //联查business表businessAddress
                .leftJoin(Business.class, "b", Business::getBusinessId, Food::getBusinessId) //左连接business表，条件为f.businessId=b.businessId
                .orderByDesc("foodId"); //排序
        //搜素条件
        if (StringUtils.isNotEmpty(foodName)){
            queryWrapper.like("foodName",foodName);
        }
        if (businessId!=null && businessId!=0){
            queryWrapper.eq("f.businessId",businessId);
        }
        return queryWrapper;
    }
}
